<?php

declare(strict_types=1);

namespace app\index\controller;

use app\model\MessageCode;
use app\model\User;
use think\captcha\facade\Captcha;
use think\facade\Session;
use think\facade\Validate;
use think\facade\View;
use think\Request;
use think\validate\ValidateRule;

class Login extends Common
{
  /**
   * 显示资源列表
   *
   * @return \think\Response
   */
  public function index()
  {
    //

    return View::fetch();
  }

  public function register()
  {
    return View::fetch();
  }

  public function captcha()
  {
    return Captcha::create();
  }

  /**
   * 显示创建资源表单页.
   *
   * @return \think\Response
   */
  public function auth()
  {
    //
    $post_data = $this->request->post();

    $validate = Validate::rule('email', ValidateRule::isRequire())
      ->rule('password', ValidateRule::isRequire())
      ->rule('vercode', function ($value) {
        return \captcha_check($value) ? true : '验证码错误';
      });

    if (!$validate->check($post_data)) {
      return json_message($validate->getError());
    }

    $model_user = User::where('email', $post_data['email'])->find();

    if (empty($model_user)) {
      return json_message('帐号不存在');
    }

    if ($model_user->getData('password') !== md5($post_data['password'] . $model_user->getData('salt'))) {
      return json_message('密码错误');
    }

    Session::set('user_id', $model_user->id);

    return $this->success('登陆成功', url('User/index'));
  }

  /**
   * 保存新建的资源
   *
   * @param  \think\Request  $request
   * @return \think\Response
   */
  public function save(Request $request)
  {
    //

    $post_data = $this->request->post();

    if (!\captcha_check($post_data['vercode'])) {
      return json_message('验证码错误');
    }

    if ($post_data['password'] != $post_data['password_repet']) {
      return json_message('两次输入的密码不正确');
    }

    $model_user = User::where('email', $post_data['email'])->find();

    if (!empty($model_user)) {
      return json_message('邮箱已被占用');
    }

    $post_data['account'] = uniqid();

    $post_data['salt'] = mt_rand(100000, 999999);

    $post_data['password'] = md5($post_data['password'] . $post_data['salt']);

    $model_user = User::create($post_data);

    Session::set('user_id', $model_user->id);

    return $this->success('注册成功', url('User/index'));
  }

  public function forget()
  {

    return View::fetch();
  }
  public function forgetSave()
  {

    // TODO:验证数据


    $post_data = $this->request->post();

    if (!MessageCode::verify($post_data['email'], $post_data['code'], 'forget_email')) {
      return json_message('验证码错误');
    }

    $model_user = User::where('email', $post_data['email'])->find();

    if (empty($model_user)) {
      return json_message('用户不存在');
    }

    $model_user->password = md5($post_data['password'] . $model_user->salt);

    $model_user->save();

    Session::set('user_id', $model_user->id);

    return json_message();
  }
  public function emailSendCode()
  {

    // TODO:增加邮箱验证
    // TODO:增加数据验证
    $post_data = $this->request->post();

    $email = $post_data['email'];

    $type = 'forget_email';

    try {
      MessageCode::buildAndSend($email, $type);
    } catch (\Throwable $th) {
      return json_message($th->getMessage());
    }

    return json_message();
  }

  /**
   * 显示指定的资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function read($id)
  {
    //
  }

  /**
   * 显示编辑资源表单页.
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function edit($id)
  {
    //
  }

  /**
   * 保存更新的资源
   *
   * @param  \think\Request  $request
   * @param  int  $id
   * @return \think\Response
   */
  public function update(Request $request, $id)
  {
    //
  }

  /**
   * 删除指定资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function delete($id)
  {
    //
  }

  public function logout()
  {
    Session::clear();

    return $this->success('退出成功', url('index'));
  }
}
